// SlideShow.js
//
// Author: Antonio Cisternino
//
// (C) 2008
//
// Made for Toscana4U (http://www.toscana4u.net)
//

function Sld_MakeShow(element, list, imgwidth, imgheight) {
  if (typeof(element) == 'string')
    element = document.getElementById(element);

  var d1 = document.createElement('div');
  var d2 = document.createElement('div');
  var d3 = document.createElement('div');
  
  var setImg = function (d, n) {
    var w = imgwidth ? 'width="' + imgwidth + '" ' : ''; 
    var h = imgheight ? 'height="' + imgheight + '" ' : ''; 
    d.innerHTML = '<img src="'+n+'" '+w+h+'alt=""/>';
  }
  
  var viewtime = 2000;
  var transitiontime = 2000;

  var ImageTransition = function (imgfrom, imgto) {
    var t = new Timeline();
    t.SetAt(viewtime, AnimeJInterp.alpha(transitiontime, 30, imgfrom, 1.0, 0.0));
    t.SetAt(viewtime, AnimeJInterp.alpha(transitiontime, 30, imgto, 0.0, 1.0));
    return t;
  }

  d1.style.position = 'absolute';
  d2.style.position = 'absolute';
  d3.style.position = 'absolute';
  d1.style.top = '0px';
  d2.style.top = '0px';
  d3.style.top = '0px';
  d1.style.width = '100%';
  d2.style.width = '100%';
  d3.style.width = '100%';
  d1.style.textAlign = 'center';
  d2.style.textAlign = 'center';
  d3.style.textAlign = 'center';
  AnimeSetOpacity(d2, 0);
  AnimeSetOpacity(d3, 0);
  
  setImg(d1, list[0]);
  setImg(d2, list[1]);
  setImg(d3, list[2]);
  
  var  idx = 3;
  var transitions;
  var inittransitions = function() {
    transitions = new Array(ImageTransition(d1, d2), ImageTransition(d2, d3), ImageTransition(d3, d1));
    transitions[0].OnEnd = function (d) { 
      transitions[1].Run();
      currentTransition = transitions[1];
      setImg(d1, list[idx]); idx = (idx + 1) % list.length;
    };
    transitions[1].OnEnd = function (d) {
      transitions[2].Run();
      currentTransition = transitions[2];
      setImg(d2, list[idx]); idx = (idx + 1) % list.length;
    };
    transitions[2].OnEnd = function (d) {
      transitions[0].Run();
      currentTransition = transitions[0];
      setImg(d3, list[idx]);
      idx = (idx + 1) % list.length;
    };
  }
  inittransitions();
  var currentTransition = transitions[0];
  
  element.appendChild(d1);
  element.appendChild(d2);
  element.appendChild(d3);
  
  return { 
    Play: function () { currentTransition.Run(); }, 
    Stop: function () { currentTransition.Stop(); },
    Pause: function () { currentTransition.Pause(); },
    Resume: function () { currentTransition.Resume(); },
    SetDisplayTime: function (t) {
      viewtime = t;
      inittransitions();
    },
    GetDisplayTime: function () {
      return viewtime;
    }, 
    SetTransitionTime: function (t) {
      transitiontime = t;
      inittransitions();
    },
    GetTransitionTime: function () {
      return transitiontime;
    }
  };
}
